﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Domain.Contracts;
using Microsoft.Practices.ServiceLocation;
using App.Domain.Handlers;
using App.Web.Models;
using System.IO;
using System.Collections;

namespace App.Web.Controllers
{
    public class SettingController : AuthorizedController
    {
        public SettingController(IUserServices userServices,
            IServiceLocator serviceLocator)
            : base(userServices, serviceLocator)
        {
        }

        //
        // GET: /Setting/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowCaseList()
        {
            var list = Using<GetPictureByCompanyId>().Execute(CurrentUser.CompanyId)
                .Select(i => new ShowCaseListViewModel() { 
                
                    PiceureId=i.Id,
                    PictureBinary=i.PictureBinary,
                    Title=i.Title,
                
                }).Take(5);

            return View(list);
        }

        public ActionResult ShowCaseEdit(string Type, string id, PictureFormModel picture)
        {
            ViewBag.Type = Type;

            if (string.IsNullOrEmpty(Type))
            {
                return View();
            }

            var a = ViewBag.Type;

            if (Type.Equals("Add"))
            {
                HttpPostedFileBase file = Request.Files["file1"];
                if (file != null)
                {
                    Stream filestream = file.InputStream;
                    byte[] pic = new byte[file.ContentLength];

                    filestream.Read(pic, 0, file.ContentLength);

                    picture.Id = Guid.NewGuid();
                    picture.CompanyId = CurrentUser.CompanyId;
                    picture.PictureBinary = pic;
                    picture.Size = file.ContentLength;
                    picture.MimeType = file.ContentType;

                    Using<CreatePicture>().Execute(CurrentUserId, picture);

                    ModelState.AddModelError("", "保存成功");
                }
                return View();

            }

            if (Type.Equals("Edit"))
            {
                if (picture.Title == null)
                {
                    var model = Using<GetPicture>().Execute(Guid.Parse(id));

                    var picturemodel = new PictureFormModel
                    {
                        Id = model.Id,
                        CompanyId = model.CompanyId,
                        Title = model.Title,
                        PictureBinary = model.PictureBinary,
                        ImageUrl = model.ImageUrl,
                        MimeType = model.MimeType,
                        Width = model.Width,
                        Height = model.Height,
                        Size = model.Size,
                        SortOrder = model.SortOrder,
                        IsLogo = model.IsLogo,
                    };
                    return View(picturemodel);
                }
                else
                {
                    var model = Using<GetPicture>().Execute(Guid.Parse(id));
                    HttpPostedFileBase file = Request.Files["file1"];
                    if (file != null)
                    {
                        Stream filestream = file.InputStream;
                        byte[] pic = new byte[file.ContentLength];

                        filestream.Read(pic, 0, file.ContentLength);
                        model.PictureBinary = pic;
                        model.Size = file.ContentLength;
                    }
                    model.Title = picture.Title;

                    var picturemodel = new PictureFormModel
                    {
                        Id = model.Id,
                        CompanyId = model.CompanyId,
                        Title = model.Title,
                        PictureBinary = model.PictureBinary,
                        ImageUrl = model.ImageUrl,
                        MimeType = model.MimeType,
                        Width = model.Width,
                        Height = model.Height,
                        Size = model.Size,
                        SortOrder = model.SortOrder,
                        IsLogo = model.IsLogo,
                    };
                    Using<UpdatePicture>().Execute(picturemodel);
                }
            }

            return View();
        }

        public ActionResult ShowCaseDelete(string id)
        {
            Using<DeletePicture>().Execute(Guid.Parse(id));
            return RedirectToAction("ShowCaseList");
        }



        #region ChangePwd
        #endregion
        public ActionResult ChangePwd(ChangePwdViewModel changepwd)
        {
            if (ModelState.IsValid)
            {

                var currentUser = Using<GetRecruiter>().Execute(this.CurrentUser.DisplayName);
                if (currentUser.Password.Equals(changepwd.CurrentPwd))
                {
                    Using<UpdateRecruiterChangePwd>().Execute(this.User.AppIdentity().UserId, changepwd.NewPwd);

                    ModelState.AddModelError("", "修改成功！");
                }
                else
                {
                    ModelState.AddModelError("", "修改失败！");
                }

            }
            return View(new ChangePwdViewModel());
        }


        #region GetImage
        public FileContentResult GetImage(Guid id)
        {
            var wantad = Using<GetPicture>().Execute(id);
            if (wantad != null)
            {
                return File(wantad.PictureBinary, String.IsNullOrWhiteSpace(wantad.MimeType) ? "image/jpeg" : wantad.MimeType);
            }
            else
            {
                return null;
            }
        }
        #endregion

        [NonAction]
        public static byte[] GetPhoto(string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);

            byte[] photo = br.ReadBytes((int)fs.Length);

            br.Close();
            fs.Close();

            return photo;
        }

    }
}
